<?php

namespace app\store\task;

use think\facade\Db;
use think\facade\Queue;
use yunwuxin\cron\Task;

class ResellTask extends Task
{
    public function configure()
    {
        $this->everyMinute();
    }

    /**
     * 执行任务
     * @return mixed
     */
    protected function execute()
    {
        $resellSession = $this->getResellSessionData();

        if (!$resellSession->isEmpty()){
            foreach ($resellSession as $v){
                $records = $this->getResellRecord($v['id']);
                if (empty($records)){
                    Db::name('user_resell_session')->where('id',$v['id'])->update(['status' => 2]);
                    continue;
                }

                if ($v['end_time']>= time()){
                    $index = array_rand($records);
                    $user_id = $records[$index]['user_id'];
                    unset($records[$index]);
                    $ids = array_column($records,'user_id');

                    Db::name('user_join_resell')
                        ->where(['resell_session_id' => $v['id'],'user_id' => $user_id])
                        ->update(['status' => 2]);
                    if (!empty($data['ids'])){
                        /**
                         * 失败处理
                         * */
                        Db::name('group_activity_record')
                            ->where('resell_session_id',$v['id'])
                            ->whereIn('user_id',$ids)
                            ->update(['status' => -1]);

                        $config = $this->getPaymentConfig($v['store_id']);
                        foreach ($records as $v1){
                            Queue::push('app\store\job\Refund',[
                                'group_order_id' => $v1['group_order_id'],
                                'payment_type' => $v1['payment_type'],
                                'out_trade_no' => $v1['out_trade_no'],
                                'config' => $config[$v1['payment_type']],
                                'refund_fee' => $v1['real_amount']
                            ],'userRefund');
                        }
                    }

                    Db::name('user_resell_session')->where('id',$v['id'])->update(['status' => 2]);
                }
            }
        }
    }

    /**
     * 获取转售数据
     * */
    protected function getResellSessionData()
    {

        $resellSession = Db::name('user_resell_session')->field('id,end_time')->where('status',1)->select();

        return $resellSession;
    }


    /**
     * 获取支付信息
     * */
    protected function getPaymentConfig($store_id)
    {

        $paymentConfig = Db::name('payment_config')
            ->field('type,config')
            ->where('store_id' ,$store_id)
            ->withAttr('config',function ($value){
                return json_decode($value,true);
            })->limit(2)
            ->select();

        if (empty($paymentConfig)){
            return  [];
        }

        $config = [];

        foreach ($paymentConfig as $v){
            $config[$v]['type'] = $v['config'];
        }

        return  $config;
    }

    /**
     * 获取转售记录信息
     * */
    protected function getResellRecord($resell_session_id)
    {
        $records = Db::name('user_join_resell')
                     ->field('a.user_id,a.group_order_id,b.payment_type,b.real_amount,b.out_trade_no')
                     ->alias('a')
                     ->join('group_order b','a.group_order_id=b.id')
                     ->where('resell_session_id',$resell_session_id)
                     ->select();

        if ($records->isEmpty()){
            return [];
        }

        return $records->toArray();
    }
}